//
//  CYFinanceInvestCouponCell.m
//  cyjf
//
//  Created by tauCross on 2017/9/26.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceInvestCouponCell.h"



@implementation CYFinanceInvestCouponCellObject

@end



@interface CYFinanceInvestCouponCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *couponDescLabel;
@property(nonatomic, strong)UIImageView *arrow;

@end


@implementation CYFinanceInvestCouponCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.text = @"使用优惠";
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        [self.titleLabel sizeToFit];
        [self.contentView addSubview:self.titleLabel];
        
        self.couponDescLabel = [[UILabel alloc] init];
        self.couponDescLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.couponDescLabel];
        
        self.arrow = [[UIImageView alloc] initWithImage:CYImage(@"public_cell_arrow")];
        [self.contentView addSubview:self.arrow];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 50;
}

- (BOOL)shouldUpdateCellWithObject:(CYFinanceInvestCouponCellObject *)object
{
    if(object.enable == 1)
    {
        self.arrow.hidden = NO;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        if(object.coupon)
        {
            self.couponDescLabel.textColor = HEXCOLOR(@"999999");
            self.couponDescLabel.text = [NSString stringWithFormat:@"%@%@", SAFE_NUMBER(object.coupon.dfinalAmount).stringValue, SAFE_STRING(object.coupon.couponUnit)];
        }
        else
        {
            self.couponDescLabel.textColor = APP_DEFAULT_COLOR_RED;
            self.couponDescLabel.text = @"发现可用优惠";
        }
    }
    else if(object.enable == 2)
    {
        self.arrow.hidden = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.couponDescLabel.textColor = HEXCOLOR(@"999999");
        self.couponDescLabel.text = @"暂无可用优惠";
    }
    else
    {
        self.arrow.hidden = NO;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        self.couponDescLabel.textColor = HEXCOLOR(@"999999");
        self.couponDescLabel.text = @"未使用";
    }
    
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.centerY = self.contentView.height / 2;
    self.titleLabel.left = 20;
    
    [self.couponDescLabel sizeToFit];
    self.couponDescLabel.right = self.contentView.width - 32;
    self.couponDescLabel.centerY = self.contentView.height / 2;
    
    self.arrow.right = self.contentView.width - 12;
    self.arrow.centerY = self.contentView.height / 2;
    
    if(self.arrow.hidden)
    {
        self.couponDescLabel.right = self.contentView.width - 20;
    }
}

@end
